/**
 * 描述：登录、注册
 * 日期：2016/6/2 0002
 **/

(function(KIT, RES) {

  var URL_TO_LOGIN = RES.URL_ROOT + "/toLogin";

  var _encrypt = function(word){
    //密钥--应和后台java解密或是前台js解密的密钥保持一致（16进制）
    var key = CryptoJS.enc.Utf8.parse("1212xxxx23291ucc");
    //偏移量
    var srcs = CryptoJS.enc.Utf8.parse(word);
    //算法
    var encrypted = CryptoJS.AES.encrypt(srcs, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
    //替换--防止值为“1”的情况
    var reg = new RegExp('/',"g");
    return encrypted.toString().replace(reg,"#");
  };

  var _decrypt = function(word){
    var key = CryptoJS.enc.Utf8.parse("1212xxxx23291ucc");
    var decrypt = CryptoJS.AES.decrypt(word, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
    return CryptoJS.enc.Utf8.stringify(decrypt).toString();
  }

  var _bindLoginEvent = function() {
    rapid("#frmLogin").find(":input").on("keypress", function(e) {
      if(e.which == rapid.keyboard.ENTER) {
        rapid("#btnLogin").trigger("click");
      }
    }).on("focus", function(e) {
      rapid("body > .r-body").css("overflow-y", "auto");

    }).on("blur", function(e) {
      rapid("body > .r-body").scrollTop(0).css("overflow-y", "hidden");
    });

    rapid("#imgCode").on("click", function() {
      rapid(this).attr("src", "/captcha?v=" + Math.random());
      rapid("#captchaInput").val("");
    });

    rapid.ui("#btnLogin").on("click", function() {
      var __s = this.disabled(true);

      var __R_login = rapid("#frmLogin"),
        __R_login_id = __R_login.findByName("loginId"),
        __R_pwd = __R_login.findByName("loginIds"),
        __R_captcha = __R_login.findByName("captcha"),
        _loginId = __R_login_id.val(),
        _pwd = __R_pwd.val(),
        _captcha = __R_captcha.val();

      //region 错误检测
      if(rapid.isEmpty(_loginId)) {
        rapid.info("请填写账号。", function() {
          __R_login_id.trigger("focus");
          __s.disabled(false);
        }, false);
        return false;
      }
      if(rapid.isEmpty(_pwd)) {
        rapid.info("请设置密码。", function() {
          __R_pwd.trigger("focus");
          __s.disabled(false);
        }, false);
        return false;
      }
      //endregion

      rapid.post({
        url: URL_TO_LOGIN,
        data: {
          loginId:_loginId,
          loginIds:_encrypt(_pwd),
          captcha:_captcha
        },
        fail: function() {
          __R_login_id.trigger("focus");
        },

        success: function(rs) {
          // Validator 层验证失败
          if(rs.adminNameMsg) {
            rapid.info(rs.adminNameMsg);
            __R_login_id.trigger("focus");
            return;
          }
          if(rs.passwordMsg) {
            rapid.info(rs.passwordMsg);
            __R_login_id.trigger("focus");
            return;
          }

          if(rs.captchaMsg) {
            rapid("#imgCode").attr("src", "/captcha?v=" + Math.random());   // captcha 被验证过以后会立即失效，更新之
            rapid.info(rs.captchaMsg, function() {
              __R_captcha.trigger("focus");
            }, false);
            return;
          }

          window.top.location.reload();
        },
        complete: function() {
          __s.disabled(false);
        }
      });
    });
  };

  rapid.Page.create()(function() {
    _bindLoginEvent();
  }).init();
})(rapid.kit, rapid.res);